package com.kingdee.eas.mx.imp.impl.app;

import java.math.BigDecimal;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.ui.face.UIRuleUtil;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.mx.basedata.enums.StateEnum;
import com.kingdee.eas.mx.imp.impl.ImPLossFactory;
import com.kingdee.eas.mx.imp.impl.ImPLossInfo;
import com.kingdee.eas.mx.imp.sc.ImpSaleContractCollection;
import com.kingdee.eas.mx.imp.sc.ImpSaleContractFactory;
import com.kingdee.eas.mx.imp.sc.ImpSaleContractInfo;

public class ImPLossControllerBean extends AbstractImPLossControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.mx.imp.impl.app.ImPLossControllerBean");


	protected IObjectPK _save(Context ctx, IObjectValue model) throws BOSException,
			EASBizException {
		ImPLossInfo iplInfo = (ImPLossInfo)model;
		
		if(iplInfo.getState().equals(StateEnum.addNew)||iplInfo.getState().equals(StateEnum.save)){
			iplInfo.setStorageF(iplInfo.getStorageFee());
			iplInfo.setDomShiResAmount(iplInfo.getNeiFreight());
			iplInfo.setNeiResAmount(iplInfo.getNeiFreit());
			iplInfo.setInsFeesResAmount(iplInfo.getCustomChange());
			iplInfo.setSeaFreResAmount(iplInfo.getFreight());
			iplInfo.setInsurResAamount(iplInfo.getInsurance());
			iplInfo.setMiscellane(iplInfo.getMiscellaneous());
			iplInfo.setImportLicen(iplInfo.getImportPermitFee());
			iplInfo.setCommissionResAmount(iplInfo.getCommission());
			iplInfo.setDeclaration(iplInfo.getClearanceRee());
			iplInfo.setState(StateEnum.save);
		}
//		ImpSaleContractInfo iscInfo = ImpSaleContractFactory.getLocalInstance(ctx).getImpSaleContractInfo("select * where impoNum = '"+iplInfo.getImportConNo().getId()+"'");
//		if(iscInfo.isWhetherAcceptance()&&iscInfo.getDiscountRate()!=null&&iscInfo.getDiscountDays()!=null&&iscInfo.getDiscountAmount()!=null){
//			BigDecimal a = new BigDecimal(0);
//			BigDecimal b = new BigDecimal(0);
//			BigDecimal c = new BigDecimal(0);
//			a = UIRuleUtil.getBigDecimal(UIRuleUtil.getBigDecimalValue(iscInfo.getDiscountRate()));
//			b = UIRuleUtil.getBigDecimal(UIRuleUtil.getBigDecimalValue(iscInfo.getDiscountDays()));
//			c = UIRuleUtil.getBigDecimal(UIRuleUtil.getBigDecimalValue(iscInfo.getDiscountAmount()));
//			iplInfo.setBillDiscountFee(a.multiply(b).multiply(c).multiply(new BigDecimal(0.01)));
//		}else{
//			iplInfo.setBillDiscountFee(null);
//		}
		ImpSaleContractCollection iscCollection = ImpSaleContractFactory.getLocalInstance(ctx).getImpSaleContractCollection("select * where impoNum = '"+iplInfo.getImportConNo().getId()+"'");
		BigDecimal abc = new BigDecimal("0");
		if(iscCollection.size()>0){
			for(int i = 0;iscCollection.size()>i;i++){
				if(iscCollection.get(i).isWhetherAcceptance()
						&&iscCollection.get(i).getDiscountRate()!=null
						&&iscCollection.get(i).getDiscountDays()!=null
						&&iscCollection.get(i).getDiscountAmount()!=null){
					BigDecimal a = new BigDecimal(0);
					BigDecimal b = new BigDecimal(0);
					BigDecimal c = new BigDecimal(0);
					a = UIRuleUtil.getBigDecimal(UIRuleUtil.getBigDecimalValue(iscCollection.get(i).getDiscountRate()));
					b = UIRuleUtil.getBigDecimal(UIRuleUtil.getBigDecimalValue(iscCollection.get(i).getDiscountDays()));
					c = UIRuleUtil.getBigDecimal(UIRuleUtil.getBigDecimalValue(iscCollection.get(i).getDiscountAmount()));
					abc = abc.add(a.multiply(b).multiply(c).multiply(new BigDecimal(0.01)));
				}
			}
		}
		
		iplInfo.setBillDiscountFee(abc);

		
		return super._save(ctx, model);
	}
    
    
    
}